<template>
  <!-- 弹窗 -->
  <el-dialog
    title="工号和分机号"
    :visible.sync="dialogVisible"
    custom-class="custom-dialog"
    width="50%"
    :before-close="handleClose"
    @opened="initData"
  >
    <el-form
      ref="form"
      class="query-more-form"
      size="mini"
      label-width="85px"
      :model="form"
    >
      <el-row :gutter="30" class="mb-20">
        <el-col :span="24"
          ><el-form-item label="用户工号"
            ><el-input
              v-model="form.UserJobNumber"
              placeholder="请输入用户工号"
              id="GongHao"
              maxlength="255" /></el-form-item
        ></el-col>

        <el-col :span="24"
          ><el-form-item label="分机号"
            ><el-input
              v-model="form.UserExtensionNumber"
              placeholder="请输入用户分机号"
              id="FenJi"
              maxlength="255" /></el-form-item
        ></el-col>
      </el-row>

      <el-row :gutter="20" class="mb-20">
        <el-button type="warning" @click="btn_Login">系统登录</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import { staticImgUrl } from "@/url.js";
import $ from "@/utils/webSocket/jquery-1.11.1.js";
import WebSocktInterface from "@/utils/webSocket/WebSocketHelper.js";
import VoiceInterfaceObject from "@/utils/webSocket/PhoneScript.js";
// import "@/utils/webSocket/layer.js";
export default {
  data() {
    return {
      form: {},
      SystemAttr: {
        CurrentUser: {
          DepartmentID: "",
          GongHao: "",
          FenJi: "",
          AgentGroupName: "",
          Params: ""
        }
      },
      str: {
        Action: "keeplive",
        CallGuid: null,
        ErrorCode: "",
        FenJi: "6001",
        GongHao: "1008",
        IsHold: false,
        Message: "2021/5/12 21:09:44",
        Params: "2021/5/12 21:09:14",
        PlatFormCode: "Default",
        State: null,
        Tel: null
      }
    };
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    handleClose() {
      this.$emit("closeDialog");
      this.form = {};
    },

    initData() {},

    btn_Login() {
      if (this.form.UserJobNumber == null) {
        this.$message.error("请输入工号");
      } else if (this.form.UserExtensionNumber == null) {
        this.$message.error("请输入分机号");
      } else {
        var url = "ws://10.37.26.12:1002";
        this.SystemAttr.CurrentUser.DepartmentID = "Default";
        this.SystemAttr.CurrentUser.GongHao = this.form.UserJobNumber;
        this.SystemAttr.CurrentUser.FenJi = this.form.UserExtensionNumber;

        let phoneMsg = {
          UserJobNumber: this.form.UserJobNumber,
          UserExtensionNumber: this.form.UserExtensionNumber
        };
        localStorage.setItem("phoneMsg", JSON.stringify(phoneMsg));

        // 关闭弹窗
        this.$emit("closeDialog");

        // 清空输入框
        this.form = {};

        var wsParams = {
          url: url,
          IsReConnect: true, //�Ƿ�����
          reConnectTime: 40, //�������
          IsKeepAlive: true, //�Ƿ�������
          KeepAliveTime: 30, //20�뷢��1��
          KeepAliveData: JSON.stringify(
            VoiceInterfaceObject.GetSendModel("keeplive")
          ), //����

          onOpen: function(e) {
            VoiceInterfaceObject.Login();
          },

          onMessage: e => {
            let index = e.data.indexOf("(");
            let methods = e.data.substring(0, index);
            let func = e.data.substring(index + 1, e.data.length - 1);

            // 获取通话结束事件
            if (methods == "Back_TalkingEnd") {
              this.$bus.$emit(
                "phoneNum",
                VoiceInterfaceObject[methods](JSON.parse(func))
              );
            } else if (methods == "Back_TelQuene") {
              let address = location.href.substr(
                location.href.indexOf("#") + 1,
                location.href.length
              );
              let data = VoiceInterfaceObject[methods](JSON.parse(func));
              if (address == "/caseDeal/dispatch") {
                this.$bus.$emit("phoneStart", data);
              } else {
                // 获取通话接听事件
                this.$confirm("有新的来电, 是否跳转到工单登记页面?", "提示", {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "warning"
                })
                  .then(() => {
                    this.$bus.$emit("phoneStart", data);
                    localStorage.setItem('number',data.tel)
                    this.$router.push("/caseDeal/dispatch");
                  })
                  .catch(() => {
                    this.$message({
                      type: "info",
                      message: "已取消跳转"
                    });
                  });
              }
            } else if (methods == "Back_EndVideoTalking") {
            } else {
              VoiceInterfaceObject[methods](JSON.parse(func));
            }
          },
          onClose: function(e) {
            // alert("啦啦啦");
          }
        };
        //��ʼ��WebSocket
        WebSocktInterface(wsParams);
      }
    }
  }
};
</script>
